// AnimalAbstractClassAndInterfaceDemo.java

// 1. 抽象类 Animal
abstract class Animal {
    public abstract void eat();
}

// 2. 接口 Afraid
interface Afraid {
    void escape();
    void makeSounds();
}

// 3. Dog 类继承 Animal 并实现 Afraid 接口
class Dog extends Animal implements Afraid {
    @Override
    public void eat() {
        System.out.println("Dog is eating dog food.");
    }

    @Override
    public void escape() {
        System.out.println("Dog is running away...");
    }

    @Override
    public void makeSounds() {
        System.out.println("Dog barks: Wang Wang!");
    }
}

// 4. Cat 类继承 Animal 并实现 Afraid 接口
class Cat extends Animal implements Afraid {
    @Override
    public void eat() {
        System.out.println("Cat is eating fish.");
    }

    @Override
    public void escape() {
        System.out.println("Cat is climbing a tree...");
    }

    @Override
    public void makeSounds() {
        System.out.println("Cat meows: Miao Miao!");
    }
}

// 5. Person 类
class Person {
    public void feed(Animal animal) {
        System.out.println("Person is feeding the animal:");
        animal.eat();
    }

    public void catchAnimal(Afraid afraidAnimal) {
        System.out.println("Person is trying to catch the animal:");
        afraidAnimal.escape();
        afraidAnimal.makeSounds();
    }
}

// 6. Main 类（包含主方法）
public class fsa {
    public static void main(String[] args) {
        // 父类引用指向子类对象（多态）
        Animal dog = new Dog();
        Animal cat = new Cat();

        System.out.println("=== 直接调用子类方法 ===");
        // 调用子类方法
        dog.eat();
        cat.eat();

        System.out.println("\n=== 使用接口引用 ===");
        // 使用接口引用
        Afraid afraidDog = new Dog();
        Afraid afraidCat = new Cat();

        Person person = new Person();

        System.out.println("\n=== 喂动物 ===");
        // 喂动物
        person.feed(dog);
        person.feed(cat);

        System.out.println("\n=== 抓动物 ===");
        // 抓动物
        person.catchAnimal(afraidDog);
        person.catchAnimal(afraidCat);

        System.out.println("\n=== 额外演示：直接使用子类对象 ===");
        // 额外演示：直接创建对象调用方法
        Dog realDog = new Dog();
        Cat realCat = new Cat();

        realDog.eat();
        realCat.eat();
        realDog.escape();
        realCat.escape();
    }
}
